/** @format */

import axios from 'axios';

const instance = axios.create({timeout: 10000});

instance.interceptors.request.use(
    config => {
        return config;
    },
    error => Promise.reject(error),
);

instance.interceptors.response.use(
    response => {
        // response还包括网络其他的内容，后端返回的内容在data中
        const res = response.data;
        return res;
    },
    error => {
        return Promise.reject(error);
    },
);

const request = (option: any) => {
    const {url, method, params, data, config} = option;
    const {headersType, responseType} = config || {};
    return instance({
        url: url,
        method,
        params,
        data,
        ...config,
        responseType: responseType,
        headers: {
            'Content-Type': headersType || 'application/json',
        },
    });
};

export const get = async <T = any>(url: string, params?: any, config?: any) => {
    const res = await request({method: 'GET', url, params, config});
    return res as unknown as T;
};
